package com.kweative.model.relation;

import com.kweative.model.Index;
import com.kweative.model.Schema;
import com.kweative.model.Table;
import com.kweative.model.column.ColumnInteger;
import com.kweative.model.exception.StructureException;

/**
 *
 * @author henrik
 */
public class RelationTable extends Table<RelationTableRow,RelationTable,Schema> {
    public ColumnInteger<RelationTable> ParentKey;
    public ColumnInteger<RelationTable> ChildKey;
    public ColumnInteger<RelationTable> Sorting;
    public Index IMain;
    public Index ISort;
    private Table parent;
    private Table child;

    public RelationTable(String name,Table parent,Table child) {
        super(name);
        this.parent = parent;
        this.child = child;
    }

    @Override
    protected void makeStructure() throws StructureException {
        IMain = new Index("Main", Index.Type.UNIQUE);
        ISort = new Index("Sort", Index.Type.INDEX);
        addIndex(IMain);
        addIndex(ISort);
        Sorting = addColumn(new ColumnInteger("Sorting"),ISort);
        ParentKey = (ColumnInteger<RelationTable>) addRelation(new RelationOne2Many(parent)).getChildColumn();
        ChildKey = (ColumnInteger<RelationTable>) addRelation(new RelationOne2Many(child)).getChildColumn();
        IMain.addColumn(ParentKey);
        IMain.addColumn(ChildKey);
    }

}